package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.*;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxBaoxiaodanAuditParam;
import cn.stylefeng.guns.sys.modular.system.service.BtyZxBaoxiaodanAuditService;
import cn.stylefeng.guns.sys.modular.system.service.ZxChengbenMingxiService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;


/**
 * 控制器
 *
 * @author zxd
 * @Date 2022-04-24 15:06:16
 */
@Controller
@RequestMapping("/btyZxBaoxiaodanAudit")
public class BtyZxBaoxiaodanAuditController extends BaseController {

    private final Integer PROJECT_REJECT = 3;
    private final Integer FINANCE_TO_AUDIT = 4;

    private final Integer FINANCE_REJECT = 5;
    private final Integer TO_PLAY = 6;

    private String PREFIX = "/modular/system/btyZxBaoxiaodanAudit";

    @Autowired
    private BtyZxBaoxiaodanAuditService btyZxBaoxiaodanAuditService;

    @Resource
    private ZxChengbenMingxiService zxChengbenMingxiService;
    /**
     * 跳转到主页面
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyZxBaoxiaodanAudit.html";
    }

    /**
     * 新增页面
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyZxBaoxiaodanAudit_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyZxBaoxiaodanAudit_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyZxBaoxiaodanAuditParam btyZxBaoxiaodanAuditParam) {
        this.btyZxBaoxiaodanAuditService.add(btyZxBaoxiaodanAuditParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyZxBaoxiaodanAuditParam btyZxBaoxiaodanAuditParam) {
        // 工程驳回 status = 3
        if (PROJECT_REJECT.equals(btyZxBaoxiaodanAuditParam.getStatus())) {
            String name = LoginContextHolder.getContext().getUser().getName();
            btyZxBaoxiaodanAuditParam.setReason(name + ": " + btyZxBaoxiaodanAuditParam.getReason());
            btyZxBaoxiaodanAuditParam.setAuditTime(new Date());
            this.btyZxBaoxiaodanAuditService.update(btyZxBaoxiaodanAuditParam);
        }

        // 工程通过 财务待审核状态 生成明细表数据 status = 4
        if (FINANCE_TO_AUDIT.equals(btyZxBaoxiaodanAuditParam.getStatus())) {
            // 事务插入
            this.btyZxBaoxiaodanAuditService.cbmxSave(btyZxBaoxiaodanAuditParam);
        }

        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyZxBaoxiaodanAuditParam btyZxBaoxiaodanAuditParam) {
        this.btyZxBaoxiaodanAuditService.delete(btyZxBaoxiaodanAuditParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyZxBaoxiaodanAuditParam btyZxBaoxiaodanAuditParam) {
        BtyZxBaoxiaodanAudit detail = this.btyZxBaoxiaodanAuditService.getById(btyZxBaoxiaodanAuditParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyZxBaoxiaodanAuditParam btyZxBaoxiaodanAuditParam) {
        return this.btyZxBaoxiaodanAuditService.findPageBySpec(btyZxBaoxiaodanAuditParam);
    }

    /**
     * 软装财务审核
     *
     * @author zxd
     * @Date 2022-04-24
     */
    @RequestMapping("/rzAudit")
    public String rzAudit() {
        return PREFIX + "/btyZxBaoxiaodanAudit_rzAudit.html";
    }

    /**
     * 财务审核
     *
     * @author zxd
     * @Date 2022-05-06
     */
    @RequestMapping("/financeAudit")
    @ResponseBody
    public ResponseData financeAudit(BtyZxBaoxiaodanAuditParam btyZxBaoxiaodanAuditParam) {
        // 财务驳回 status = 5
        if (FINANCE_REJECT.equals(btyZxBaoxiaodanAuditParam.getStatus())) {
            this.btyZxBaoxiaodanAuditService.financeReject(btyZxBaoxiaodanAuditParam);
        }

        // 财务通过 待打款 status = 6
        if (TO_PLAY.equals(btyZxBaoxiaodanAuditParam.getStatus())) {
            this.btyZxBaoxiaodanAuditService.financeAgree(btyZxBaoxiaodanAuditParam);
        }

        return ResponseData.success();
    }

    /**
     * 财务保存
     *
     * @author zxd
     * @Date 2022-05-19
     */
    @RequestMapping("/financeSave")
    @ResponseBody
    public ResponseData financeSave(BtyZxBaoxiaodanAuditParam btyZxBaoxiaodanAuditParam) {
        System.out.println(btyZxBaoxiaodanAuditParam.getBtyZxChengbenMingxiId());
        ZxChengbenMingxi zxChengbenMingxi = zxChengbenMingxiService.getById(btyZxBaoxiaodanAuditParam.getBtyZxChengbenMingxiId());
        if (!"3".equals(zxChengbenMingxi.getStatus())) {
            return ResponseData.error("待审批状态才可保存");
        }
        String name = LoginContextHolder.getContext().getUser().getName();
        Long id = LoginContextHolder.getContext().getUser().getId();
        LambdaUpdateWrapper<ZxChengbenMingxi> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ZxChengbenMingxi::getBtyZxChengbenMingxiId, btyZxBaoxiaodanAuditParam.getBtyZxChengbenMingxiId());
        wrapper.set(ZxChengbenMingxi::getUpdateUserName, name);
        wrapper.set(ZxChengbenMingxi::getUpdateUser, id);
        wrapper.set(ZxChengbenMingxi::getCreateTime, new Date());
        wrapper.set(ZxChengbenMingxi::getUpdateTime, new Date());
        wrapper.set(ZxChengbenMingxi::getJine, btyZxBaoxiaodanAuditParam.getRemitAmount());
        wrapper.set(ZxChengbenMingxi::getBtyyBxdaId, btyZxBaoxiaodanAuditParam.getPayeeCard());
        wrapper.set(ZxChengbenMingxi::getBtyyBxdaIdView, btyZxBaoxiaodanAuditParam.getPayee());
        zxChengbenMingxiService.update(wrapper);
        this.btyZxBaoxiaodanAuditService.update(btyZxBaoxiaodanAuditParam);
        return ResponseData.success();
    }
}


